import 'package:flutter/material.dart';

class GenericForm extends StatefulWidget {
  final List<String>? labels;

  final Function(List<String> values)? onpress;

  const GenericForm({
    super.key,
    this.labels,
    this.onpress,
  });

  @override
  _GenericFormState createState() => _GenericFormState();
}

class _GenericFormState extends State<GenericForm> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  late List<String> formValues;

  @override
  void initState() {
    super.initState();
    formValues = List<String>.filled(widget.labels!.length, '');
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Container(
        height: widget.labels!.length * 70 + 100,
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Positioned(
                  top: 0,
                  child: Container(
                    width: 20,
                    height: 6,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        color: Colors.grey),
                  )),
              Padding(
                padding: const EdgeInsets.only(bottom: 10, top: 20),
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: widget.labels!.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: TextFormField(
                        decoration: InputDecoration(
                          labelText: widget.labels![index],
                          alignLabelWithHint: true,
                          contentPadding: const EdgeInsets.symmetric(
                              vertical: 10.0, horizontal: 16.0),
                          border: const OutlineInputBorder(),
                        ),
                        onSaved: (value) {
                          formValues[index] = value!;
                        },
                      ),
                    );
                  },
                ),
              ),
              Positioned(
                left: 0,
                right: 0,
                bottom: 10,
                child: Container(
                  width: 100,
                  height: 40,
                  decoration: BoxDecoration(
                    color: Colors.orange,
                    borderRadius: BorderRadius.circular(10),
                  ),
                  alignment: Alignment.center,
                  child: TextButton(
                      onPressed: () {
                        if (_formKey.currentState!.validate()) {
                          _formKey.currentState!.save();
                          widget.onpress!(formValues);
                        }
                      },
                      child: const Text(
                        "添加",
                        style: TextStyle(color: Colors.white),
                      )),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
